﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

/// <summary>
/// 管理事件的类
/// </summary>
public static class EventMgr
{
    /// <summary>
    /// 事件哈希表
    /// </summary>
    private static Hashtable eventHashtable = new Hashtable();

    static EventMgr()
    {
        foreach (int i in Enum.GetValues(typeof(GameTypeDefine.EVENT_ID)))
        {
            eventHashtable.Add(i, new EventDelegate());
        }
    }

    /// <summary>
    /// 为事件添加监听者
    /// </summary>
    /// <param name="id"></param>
    /// <param name="func"></param>
    public static void AddEventListener(GameTypeDefine.EVENT_ID id, UnityAction<GameTypeDefine.EVENT_ID, MyEventArgs> func)
    {
        EventDelegate eventDelegate = (EventDelegate)eventHashtable[(int)id];

        eventDelegate.AddListener(func);
    }

    /// <summary>
    /// 移除监听者
    /// </summary>
    /// <param name="id"></param>
    /// <param name="func"></param>
    public static void RemoveEventListener(GameTypeDefine.EVENT_ID id, UnityAction<GameTypeDefine.EVENT_ID, MyEventArgs> func)
    {
        EventDelegate eventDelegate = (EventDelegate)eventHashtable[(int)id];

        eventDelegate.RemoveListener(func);
    }

    /// <summary>
    /// 触发事件
    /// </summary>
    /// <param name="id"></param>
    /// <param name="args"></param>
    public static void InvokeEvent(GameTypeDefine.EVENT_ID id, MyEventArgs args)
    {
        EventDelegate eventDelegate = (EventDelegate)eventHashtable[(int)id];

        eventDelegate.Invoke(id, args);
    }
}
